﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace Server
{
    public class RssFeedService : IRssFeedService
    {
        private Server _server = new Server();

        public Session startSession(String email, String password, ServiceDevice version)
        {
            Session session = new Session();
            int res = _server.Auth(email, password, version);
            session.version = version;
            if (res > 0)
            {
                session.id = res;
                session.status = ServiceStatus.Success;
            }
            else
            {
                session.id = 0;
                session.status = (ServiceStatus)res;
            }
            return session;
        }

        public ServiceStatus endSession(Session session)
        {
            if (_server.closeSession(session.id) == 1)
                return ServiceStatus.Success;
            else
                return ServiceStatus.ServerError;
        }

        public List<RssFeed> ListRssFeed(Session session)
        {
            SessionEntity s = _server.getSession(session.id);
            if (s != null){
               UserEntity u = s.user1;
               List<RssFeed> list =  _server.listRssFeed(u, false);
               return list;
            }
            return null;
        }

        public List<RssFeed> ListPublicRssFeed(Session session)
        {
            SessionEntity s = _server.getSession(session.id);
            if (s != null)
            {
                UserEntity u = s.user1;
                List<RssFeed> list = _server.listRssFeed(u, true);
                return list;
            }
            return null;
        }

        public List<RssFeedItem> ListRssFeedItem(Session session, RssFeed rss_feed)
        {
            SessionEntity s = _server.getSession(session.id);
            if (s != null)
            {
                UserEntity u = s.user1;
                List<RssFeedItem> list = _server.listRssFeedItem(rss_feed, u);
                return list;
            }
            return null;
        }

        public List<RssFeedItem> ListLastRssFeedItem(Session session, bool no_read = false)
        {
            SessionEntity s = _server.getSession(session.id);
            if (s != null)
            {
                UserEntity u = s.user1;
                List<RssFeedItem> list = _server.listLastRssFeedItem(u, no_read);
                return list;
            }
            return null;
        }

        public RssFeed Subscribe(Session session, RssFeed rss_feed)
        {
            SessionEntity s = _server.getSession(session.id);
            if (s != null)
            {
                UserEntity u = s.user1;
                return _server.Subscribe(rss_feed, u);
            }
            return null;
        }

        public RssFeed RefreshRssFeed(Session session, RssFeed rss_feed)
        {
            SessionEntity s = _server.getSession(session.id);
            if (s != null)
            {
                return _server.RefreshRssFeed(rss_feed);
            }
            return null;
        }

        public RssFeedItem ReadSate(Session session, RssFeedItem item, bool read)
        {
            SessionEntity s = _server.getSession(session.id);
            if (s != null)
            {
                UserEntity u = s.user1;
                return _server.ReadState(u, item, read);
            }
            return null;
        }
    }
}
